package com.mazaiting.redeye.utils

import net.sourceforge.pinyin4j.PinyinHelper
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination
import java.util.regex.Pattern


/***
 *
 *
 *                                                    __----~~~~~~~~~~~------___
 *                                   .  .   ~~//====......          __--~ ~~
 *                   -.            \_|//     |||\\  ~~~~~~::::... /~
 *                ___-==_       _-~o~  \/    |||  \\            _/~~-
 *        __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *    _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *  .~       .~       |   \\ -_    /  /-   /   ||      \   /
 * /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 * |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *          '         ~-|      /|    |-~\~~       __--~~
 *                      |-~~-_/ |    |   ~\_   _-~            /\
 *                           /  \     \__   \/~                \__
 *                       _--~ _/ | .-~~____--~-/                  ~~==.
 *                      ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                 -_     ~\      ~~---l__i__i__i--~~_/
 *                                 _-~-__   ~)  \--______________--~~
 *                               //.-~~~-~_--~- |-------~~~~~~~~
 *                                      //.-~~~--\
 *                               神兽保佑
 *                              代码无BUG!
 * @author mazaiting
 * @date 2019-08-01
 * @description 汉字转拼音工具类
 */
object PinYinHelper {


    //多音字替换为我们希望的同样发音的单音字，可以继续增加
    var specialHanzi: MutableMap<String, String> = HashMap()

    /**
     * 得到 全拼
     *
     * @param source
     * @return
     */
    fun getPingYin(source: String): String? {
        var src = source
        val t1: CharArray?

        if (src.trim { it <= ' ' }.isEmpty()) {
            return null
        }
        //遇到多音字，先转换为单音字（根据指定的表），再取拼音
        val firstChar = src.trim { it <= ' ' }.substring(0, 1)
        if (specialHanzi.containsKey(firstChar)) {
            src = src.replace(firstChar, specialHanzi[firstChar]!!)
        }

        t1 = src.toCharArray()
        var t2: Array<String?>
        val t3 = HanyuPinyinOutputFormat()
        t3.caseType = HanyuPinyinCaseType.LOWERCASE
        t3.toneType = HanyuPinyinToneType.WITHOUT_TONE
        t3.vCharType = HanyuPinyinVCharType.WITH_V
        var t4 = ""
        val t0 = t1.size
        try {
            for (i in 0 until t0) {
                // 判断是否为汉字字符
                if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+".toRegex())) {
                    t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3)
                    t4 += t2[0]
                } else {
                    t4 += Character.toString(t1[i])
                }
            }
            return t4.toUpperCase()
        } catch (e1: BadHanyuPinyinOutputFormatCombination) {
            e1.printStackTrace()
        }

        return t4.toUpperCase()
    }

    fun getFirstPingYin(source: String): String? {
        var src = source
        val t1: CharArray?
        if (src.isEmpty()) {
            return null
        }

        if (!isHanzi(src)) {
            return src.substring(0, 1).toUpperCase()
        }

        //遇到多音字，先转换为单音字（根据指定的表），再取拼音
        val firstChar = src.substring(0, 1)
        src = if (specialHanzi.containsKey(firstChar)) {
            specialHanzi[firstChar].toString()
        } else {
            firstChar
        }

        t1 = src.toCharArray()
        var t2: Array<String?>
        val t3 = HanyuPinyinOutputFormat()
        t3.caseType = HanyuPinyinCaseType.LOWERCASE
        t3.toneType = HanyuPinyinToneType.WITHOUT_TONE
        t3.vCharType = HanyuPinyinVCharType.WITH_V
        var t4 = ""
        val t0 = t1.size
        try {
            for (i in 0 until t0) {
                // 判断是否为汉字字符
                if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+".toRegex())) {
                    t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3)
                    t4 += t2[0]
                } else {
                    t4 += Character.toString(t1[i])
                }
            }
            return t4.toUpperCase()
        } catch (e1: BadHanyuPinyinOutputFormatCombination) {
            e1.printStackTrace()
        }

        return t4.toUpperCase()
    }

    /**
     * 得到首字母
     *
     * @param str
     * @return
     */
    fun getHeadChar(src: String): String? {
        var str = src
        if (str.trim { it <= ' ' }.isEmpty()) {
            return null
        }
        //遇到多音字，先转换为单音字（根据指定的表），再取拼音
        val firstChar = str.trim { it <= ' ' }.substring(0, 1)
        if (specialHanzi.containsKey(firstChar)) {
            str = specialHanzi[firstChar].toString()
        }

        var convert = ""
        val word = str[0]
        val pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word)
        if (pinyinArray != null) {
            convert += pinyinArray[0][0]
        } else {
            convert += word
        }
        return convert.toUpperCase()
    }

    /**
     * 得到中文首字母缩写
     *
     * @param str
     * @return
     */
    fun getPinYinHeadChar(str: String): String? {
        if (str.trim { it <= ' ' }.isEmpty()) {
            return null
        }
        var convert = ""
        for (j in 0 until str.length) {
            val word = str[j]
            val pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word)
            convert +=
                if (pinyinArray != null) {
                    pinyinArray[0][0]
                } else {
                    word
                }
        }
        return convert.toUpperCase()
    }

    init {
        specialHanzi["重"] = "虫"//重->虫
        specialHanzi["贾"] = "甲"//贾->甲
        specialHanzi["瞿"] = "渠"//瞿->渠
        specialHanzi["单"] = "擅"//单->擅
        specialHanzi["沈"] = "审"//沈->审
        specialHanzi["解"] = "谢"//解->谢
        specialHanzi["俞"] = "于"//俞->于
        specialHanzi["曾"] = "增"//曾->增
    }


    fun isHanzi(str: String): Boolean {
        val c = str[0]
        // 正则表达式，判断首字母是否是英文字母
        val pattern = Pattern.compile("[\\u4E00-\\u9FA5]+")
        return pattern.matcher(c + "").matches()
    }

}